﻿/****************************************************************
*   作者：corer
*   创建时间：2018/2/6 11:29:28
*   描述说明：
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace Model
{
  /// <summary>
  /// 物品管理
  /// </summary>
  public partial class UIInventory : MonoBehaviour
  {
    /// <summary>
    /// 数量显示
    /// </summary>
    public Text NumberText;

    /// <summary>
    /// 整理按钮
    /// </summary>
    public Button button;

    /// <summary>
    /// 格子数量
    /// </summary>
    protected List<UISlot> slots = new List<UISlot>();

    /// <summary>
    /// 整理时，临时保存数据。
    /// </summary>
    private List<ItemViewModel> backList = new List<ItemViewModel>();

    protected virtual void Awake()
    {
      this.FindAllSlot();
      this.UpdateNumerText();
      this.button.onClick.AddListener(Sort);
    }

    #region 添加Item相关方法

    /// <summary>
    /// UI上新添加一个物品
    /// </summary>
    /// <param name="service"></param>
    /// <param name="vm"></param>
    protected virtual void ItemAdd(ItemViewModel vm)
    {
      UISlot slot = null;

      if (vm.Item.Capacity > 1)
      {
        slot = this.FindEqTypeSlot(vm);
        if (slot != null)
        {
          slot.StoreItem(vm);
          this.UpdateNumerText();
          return;
        }
      }

      slot = this.FindIsEmptySlot();
      if (slot != null)
      {
        slot.StoreItem(vm);
        this.UpdateNumerText();
      }
      else
      {
        Debug.Log("没有空位....");
      }

    }

    /// <summary>
    /// 查找所有格子
    /// </summary>
    protected void FindAllSlot()
    {
      Transform tf = transform.Find("Rect/Slots");
      UISlot[] arr = tf.GetComponentsInChildren<UISlot>();
      this.slots.AddRange(arr);
    }

    /// <summary>
    /// 查找一个空格子
    /// </summary>
    protected UISlot FindIsEmptySlot()
    {
      foreach (var slot in slots)
      {
        if (slot.IsEmpty)
        {
          return slot;
        }
      }
      return null;
    }

    /// <summary>
    /// 查找相同类型的格子
    /// </summary>
    /// <returns></returns>
    protected UISlot FindEqTypeSlot(ItemViewModel vm)
    {
      foreach (var slot in slots)
      {
        // 查找相同的物品格子
        if (!slot.IsEmpty && slot.VM.Item.ID == vm.Item.ID)
        {
          return slot;
        }
      }
      return null;
    }

    /// <summary>
    /// 更新占用格子数量
    /// </summary>
    public void UpdateNumerText()
    {
      int count = 0;

      foreach (UISlot slot in slots)
      {
        if (!slot.IsEmpty)
        {
          count++;
        }
      }
      this.NumberText.text = count + "/" + slots.Count;
    }
    #endregion

    #region 背包 整理
    public void Sort()
    {
      foreach (var item in slots)
      {
        if (item.IsEmpty) continue;
        backList.Add(item.VM);
        item.Dispose();
      }

      for (int i = 0; i < this.backList.Count - 1; i++)
      {
        for (int j = 0; j < this.backList.Count - i - 1; j++)
        {
          if (this.backList[j].Item.ID > this.backList[j + 1].Item.ID)
          {
            var temp = this.backList[j];
            this.backList[j] = this.backList[j + 1];
            this.backList[j + 1] = temp;
          }
        }
      }

      for (int i = 0; i < this.backList.Count; i++)
      {
        this.slots[i].StoreItem(this.backList[i]);
      }

      backList.Clear();
    }
    #endregion

  }
}